﻿#region Copyright (c) 2012-10, Olaf Kober <amarok.projects@gmail.com>
//================================================================================
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//================================================================================
#endregion

using System;


namespace Amarok
{
	/// <summary>
	/// This type provides extension methods for <see cref="System.Exception"/>.
	/// </summary>
	public static class ExceptionExtensions
	{
		#region ++ Public Interface (FindBaseException) ++

		/// <summary>
		/// Returns the supplied exception or one of its inner exceptions matching the specified exception type. 
		/// This method does support AggregateException and will iterate through all inner exceptions and all
		/// inner AggregateExceptions.
		/// 
		/// This method is useful for finding a specific exception type that is saved as one of the inner exceptions, 
		/// without relying onto whether it's the first, second or n-th inner exception.
		/// </summary>
		/// 
		/// <typeparam name="T">
		/// The type of exception the method should return.</typeparam>
		/// 
		/// <param name="exception">
		/// The outer-most exception whose inner exceptions are searched. If the supplied exception itself matches 
		/// the requested exception type, then it is returned. A null value is valid.</param>
		/// 
		/// <returns>
		/// A reference to the first exception that matches the specified exception type, if the supplied exception 
		/// or one of its inner exceptions matches; otherwise null.
		/// </returns>
		public static T FindBaseException<T>(this Exception exception)
			where T : Exception
		{
			return Helper.FindBaseException<T>(exception);
		}

		#endregion

		#region ++ Public Interface (FindBaseExceptions) ++

		/// <summary>
		/// Searches through all inner exceptions and returns a collection of exceptions that match the given 
		/// exception type. This method does support AggregateException and will iterate through all inner exceptions 
		/// and all inner AggregateExceptions.
		/// </summary>
		/// 
		/// <typeparam name="T">
		/// The type of exception the method should return.</typeparam>
		/// 
		/// <param name="exception">
		/// The outer-most exception whose inner exceptions are searched. If the supplied exception itself matches 
		/// the requested exception type, then it is returned too. A null value is valid.</param>
		/// 
		/// <returns>
		/// A collection of exceptions that match the specified exception type, if the supplied exception or at least 
		/// one of its inner exceptions matches; otherwise an empty collection is returned.
		/// </returns>
		public static T[] FindBaseExceptions<T>(this Exception exception)
			where T : Exception
		{
			return Helper.FindBaseExceptions<T>(exception);
		}

		#endregion

		#region ++ Public Interface (GetInnerMostException) ++

		/// <summary>
		/// Returns the inner-most exception (root cause) of the supplied exception.
		/// </summary>
		/// 
		/// <param name="exception">
		/// The exception whose inner-most exception should be returned. Null is a valid value.</param>
		/// 
		/// <returns>
		/// A reference to the inner-most exception of the supplied exception, or null if a null reference 
		/// was supplied.</returns>
		public static Exception GetInnerMostException(this Exception exception)
		{
			return Helper.GetInnerMostException(exception);
		}

		#endregion

	}
}
